<template>
  <el-dialog title="分配应用" :visible.sync="visible" width="900px" top="5vh" append-to-body>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item prop="categoryId">
        <el-select
          v-model="queryParams.categoryId"
          placeholder="请选择应用分类"
          style="width: 240px"
          @change="handleTypeChange"
        >
          <el-option
            v-for="(type, i) in filteredTypeList"
            :key="type.categoryId"
            :label="type.categoryName"
            :value="type.categoryId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="openSelectApp"
          v-hasPermi="['system:role:add']"
        >添加应用
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="cancelAuthUserAll"
          v-hasPermi="['system:role:remove']"
        >批量取消授权
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="图片" align="center" prop="dictId">
        <template slot-scope="scope">
          <img :src="getImg(scope.row.icon)" alt="" width="60" height="60">
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column label="应用分类" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{getCategoryType(scope.row.categoryId)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="应用小类" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.application_small_type" :value="scope.row.dictDataId"/>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="cancelAuthUser(scope.row)"
            v-hasPermi="['system:role:remove']"
          >取消授权
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <select-app ref="select" :roleId="queryParams.roleId" @ok="handleQuery" />

  </el-dialog>
</template>

<script>
  import {getTypeList, authAppCancel, getSelectApp} from "@/api/system/application.js"
  import selectApp from './selectApp'
  export default {
    name: "AuthApp",
    dicts: ['application_small_type'],
    data() {
      return {
        visible: false,
        // 遮罩层
        loading: true,
        // 选中应用组
        appIds: [],
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          roleId: undefined,
          categoryId: undefined
        },
        typeList: [],
        appList: [],
        total: 0
      };
    },
    components: { selectApp },
    methods: {

      open(id) {
        // const roleId = this.$route.params && this.$route.params.roleId;
        const roleId = id
        this.visible = true
        if (roleId) {
          this.queryParams.roleId = roleId;
          this.getCategoryList();
          this.getList()
        }
      },

      getCategoryType(id) {
        let name = ""
        this.typeList.forEach(e => {
          if(e.categoryId === id) {
            name = e.categoryName
          }
        })
        return name;
      },

      //获取分类列表
      getCategoryList() {
        getTypeList().then(res => {
          if(res.code === 200) {
            if(res.data.length > 0) {
              this.typeList = res.data
              /*this.queryParams.categoryId = res.data[0].categoryId
              this.getList()*/
            }
          }else {
            this.$message.error(res.msg)
          }
        }).catch(err => {
          this.$message.error(err)
        })
      },

      //获取应用列表
      getList() {
        getSelectApp(this.queryParams).then(res => {
          if(res.code === 200) {
            this.loading = false
            this.appList = res.rows
            this.total = res.total
          }else {
            this.$message.error(res.msg)
          }
        }).catch(err => {
          this.$message.error(err)
        })
      },

      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },

      handleSelectionChange(selection) {
        this.appIds = selection.map(item => item.applicationInfoId)
        console.log(this.appIds)
        this.multiple = !selection.length
      },

      /** 取消授权按钮操作 */
      cancelAuthUser(row) {
        let ids = []
        const roleId = this.queryParams.roleId;
        ids.push(row.applicationInfoId)
        this.$modal.confirm('确认要取消该应用"' + row.name + '"角色吗？').then(function() {
          return authAppCancel({ roleId: roleId, applicationIds: ids});
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("取消授权成功");
        }).catch(() => {});
      },
      /** 批量取消授权按钮操作 */
      cancelAuthUserAll(row) {
        const roleId = this.queryParams.roleId;
        const ids = this.appIds;
        this.$modal.confirm('是否取消选中用户授权数据项？').then(function() {
          return authAppCancel({ roleId: roleId, applicationIds: ids });
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("取消授权成功");
        }).catch((err) => {
          console.log(err)
        });
      },

      //切换分类
      handleTypeChange(e) {
        this.queryParams.categoryId = e
        this.queryParams.pageNum = 1
        this.getList()
      },

      //获取图片
      getImg(path) {
        let url = process.env.VUE_APP_BASE_API + path
        return url
      },

      openSelectApp() {
        this.$refs.select.show();
      },
    },
    computed: {
    filteredTypeList() {
      return this.typeList.filter(type => type.categoryId !== 22);
    }
  }
  }
</script>

<style lang="scss" scoped>

</style>
